package com.thxr.sso.server.util;

import org.springframework.util.DigestUtils;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;


public class MD5Tools {
    public static final String MD5(String pwd) {
        char[] md5String = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

        try {
            byte[] btInput = pwd.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = md5String[byte0 >>> 4 & 0xF];
                str[k++] = md5String[byte0 & 0xF];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }


    public static String encodeStr(byte[] plainText) {
        String encode = (new BASE64Encoder()).encode(plainText);
        return encode;
    }


    public static void main(String[] args) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        System.out.println(timestamp);
        String token = MD5("test1"+"c9f18383a32f6bf51669b881ed8535ee"+timestamp);
        System.out.println(token);

        String s = DigestUtils.md5DigestAsHex(("test1"+"c9f18383a32f6bf51669b881ed8535ee"+timestamp).getBytes());
        System.out.println(s);
    }
}